package com.card.admin.pojo;

import java.util.List;
import java.util.Map;
import java.util.Vector;

import com.card.common.CardContants;
import com.card.game.BasePlayer;
import com.card.game.Card;
import com.card.game.CardType;
import com.card.game.CardUtils;
import com.card.game.Player;
import com.card.game.room.SHRoom;
import com.card.user.User;
import com.card.user.UserUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xiaoao.net.db.Entity;
import com.xiaoao.net.db.Key;
import com.xiaoao.net.db.Query;
import com.xiaoao.net.db.mongo.MongodbManager;
import com.xiaoao.net.utils.PubFunc;

/**   
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author siyunlong  
 * @date 2016年11月28日 上午11:33:59 
 * @version V1.0   
 */
public class SuperPlayerManager {
	public static final String T_Admin_DATA = "superplayer";
	private static final SuperPlayerManager instance = new SuperPlayerManager();
	public static SuperPlayerManager getInstance() {
		return instance;
	}
	
//	private int seed = 50;
	private List<SuperPlayer> superList = Lists.newArrayList();
	private Map<String,SuperPlayer> superMap = Maps.newConcurrentMap();

	public void init() {
		Query query = new Query(SuperPlayer.T_Table);
		query.setLimit(1000000);
		List<Entity> els = MongodbManager.getInstance().queryEntity(query);
		for (Entity entity : els) {
			SuperPlayer sp = new SuperPlayer(entity);
			addPlayer(sp);
		}
	}
	
	private void addPlayer(SuperPlayer p) {
		superList.add(p);
		superMap.put(p.getId(), p);
	}
	
	public void addUser(String userId) {
		User user = UserUtils.getUserByPrimaryKey(userId);
		if(user != null) {
			SuperPlayer sp = new SuperPlayer(user.getPrimaryKey(),user.userExtra().getNickName(),0.5f);
			sp.store();
			addPlayer(sp);
		}
	}
	public void changeUserRate(String userId,String rate) {
		SuperPlayer sp = superMap.get(userId);
		if(sp != null) {
			double nr = PubFunc.parseDouble(rate);
			sp.setPro(nr);
			sp.store();
		}
	}
	public boolean isSuperPlayer(String userId) {
		return superMap.containsKey(userId);
	}
	public List<SuperPlayer> getSuperList() {
		return superList;
	}

	public void removeSuperPlayer(String userId) {
		SuperPlayer superPlayer = superMap.remove(userId);
		if(superPlayer != null) {
			superList.remove(superPlayer);
			MongodbManager.getInstance().removeEntity(new Key(SuperPlayer.T_Table, userId));
		}
	}
	
	private BasePlayer getSuperPlayer(SHRoom room) {
		Vector<BasePlayer> plist = room.getPlayerList();
		for (BasePlayer player : plist) {
			if(isSuperPlayer(player.getUserId())) {
				return player;
			}
		}
		return null;
	}
	
	public Card findJinPai(Vector<Card> tableCards) {
		int index = CardContants.YoujinNum;
		Card card = tableCards.get(index);
		card.setLiujuCard(true);
		return getJinCard(card);
	}
	
	public static Card getJinCard(Card card) {
		int type = card.getType();
		int index = card.getIndex();
		if(card.getType() <= CardType.BING) {
			if(card.getIndex() == 9) {
				return new Card(type,1);
			}else{
				return new Card(type,index+1);
			}
		}
		if(type == CardType.DONG) {
			return new Card(CardType.XI,0);
		}
		if(type == CardType.XI) {
			return new Card(CardType.NAN,0);
		}
		if(type == CardType.NAN) {
			return new Card(CardType.BEI,0);
		}
		if(type == CardType.BEI) {
			return new Card(CardType.ZHONG,0);
		}
		if(type == CardType.ZHONG) {
			return new Card(CardType.FA,0);
		}
		if(type == CardType.FA) {
			return new Card(CardType.BAI,0);
		}
		if(type == CardType.BAI) {
			return new Card(CardType.DONG,0);
		}
		return card;
	}
	
	public static void main(String[] args) {
	}
}
